package edu.chl.hajo.shop.core;

import edu.chl.hajo.shop.utils.AbstractDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

/**
 * All products
 *
 * @author hajo
 */
public  class ProductCatalogue extends AbstractDAO<Product, Long>
        implements IProductCatalogue {

    private ProductCatalogue(String name) {
        super(Product.class, name);
    }

    // Factory method
    public static IProductCatalogue newInstance(String pu) {
        return new ProductCatalogue(pu);
    }

  
 
    @Override
    public List<Product> getByName(String name) {
     
        EntityManager em = super.getEM();
        String query = "select p from Product p where p.name = ?1";
        TypedQuery<Product> q = em.createQuery(query, Product.class);
        q.setParameter(1, name);
        List<Product> ps = q.getResultList();
        return ps;
        
    }

  
    @Override
    public List<Product> getById(Long id) {
    
        EntityManager em = super.getEM();
        String query = "select p from Product p where p.id = ?1";
        TypedQuery<Product> q = em.createQuery(query, Product.class);
        q.setParameter(1, id);
        List<Product> ps = q.getResultList();
        return ps;
      
    }

 
    @Override
    public List<Product> getByPrice(Double price) {
     
        EntityManager em = super.getEM();
        String query = "select p from Product p where p.price = ?1";
        TypedQuery<Product> q = em.createQuery(query, Product.class);
        q.setParameter(1, price);
        List<Product> ps = q.getResultList();
        return ps;
      
    }
}
